# Maintainer: Antonio Rojas <arojas@archlinux.org>
# Contributor: Rémy Oudompheng <remy@archlinux.org>

pkgbase=texlive-texmf
pkgname=(texlive-doc texlive-meta)
# generate collections with ./list-collections
_collections=( basic bibtexextra binextra context fontsextra fontsrecommended fontutils formatsextra games humanities langarabic langchinese langcjk langcyrillic langczechslovak langenglish langeuropean langfrench langgerman langgreek langitalian langjapanese langkorean langother langpolish langportuguese langspanish latex latexextra latexrecommended luatex mathscience metapost music pictures plaingeneric pstricks publishers xetex )
for _coll in ${_collections[@]}; do
  pkgname+=(texlive-$_coll)
done
_rev=66594
pkgver=2023.$_rev
pkgrel=19
pkgdesc='TeX Live - '
license=(GPL)
arch=(any)
depends=(texlive-bin)
makedepends=(subversion)
url='http://tug.org/texlive/'
source=(svn://tug.org/texlive/tags/texlive-2023.0/Master/texmf-dist#revision=$_rev
        svn://tug.org/texlive/tags/texlive-2023.0/Master/tlpkg#revision=$_rev
        svn://tug.org/texlive/tags/texlive-2023.0/Master/bin/x86_64-linux#revision=$_rev
        09-texlive-fonts.conf
        texmf.cnf.patch
        texmfcnf.lua.patch
        70-mktexlsr.hook
        mktexlsr.script
        71-texlive-language.hook
        texlive-language.script
        72-texlive-fmtutil.hook
        texlive-fmtutil.script
        73-texlive-updmap.hook
        texlive-updmap.script)
sha256sums=('SKIP'
            'SKIP'
            'SKIP'
            '5e79c40cf3ab93348fc89e97890198601767ea2c8fea89ea76088c17a2b35962'
            '6baf9070b96a70e78e3b8365a01fbf4637f76d70f9db2fde5d36640ee518c40a'
            '7bf4b47ed7562dc8967a532fb13aa3c1194db1b74d7725f303612fac54750da9'
            'c76f01fe2a42e5860f7d0b2f16a4fc09101e1a14ea7488985e914cda749f1a21'
            '05afeae62a5d4c9de79c838c9636e2aefe9ad1d6b787fed4e5930c13baf60eba'
            'e6d399faee55ba461cf7e617f2369f5c516de292b28afc6665c9e3fe2b821973'
            'c64c2a6371e94b0f67799c0ac84ea74d8edbc181b26672aa15b8132ec5fbabc3'
            'b641550fe7727422b6964d505db7dbc35b3680a9d47b8d97ac550828bdb9bac7'
            'f96e9f815fa0a4b85e677f2a9215d9106b8abe46eceb3f3e36a6c76eda3e4a85'
            '2141c0842668fb937fd21ca2fae39b642c9665656e404a0d4ee7bdc477bf51fe'
            'ee6e76192a5ad880a2152cd7900b86c8465239fb228045a2f8360b0d7a449f4a')
options=(!strip) # Nothing to strip, save packaging time

prepare() {
# Customize configuration
  patch -d texmf-dist/web2c -p0 < texmf.cnf.patch
  patch -d texmf-dist/web2c -p0 < texmfcnf.lua.patch

# Copy files where format and maps will be extracted from
  cp texmf-dist/web2c/{fmtutil.cnf,updmap.cfg} .
  cp texmf-dist/tex/generic/config/language.{dat,dat.lua,def} .
  
# Split files per package
  for _coll in ${_collections[@]}; do
    echo -ne "splitting collection ${_coll}"
    # extract description
    _desc=`sed -e "0,/^name collection-${_coll}$/d;/^$/Q" tlpkg/texlive.tlpdb | sed -ne 's|^shortdesc ||p'`
    echo $_desc > pkgdesc-$_coll
    # extract depends
    _pkgs=`sed -e "0,/^name collection-${_coll}$/d;/^$/Q" tlpkg/texlive.tlpdb | sed -ne 's|^depend ||p'`
    _prog=0
    _total=`echo $_pkgs | wc -w`
    for _pkg in $_pkgs; do
      _prog=$(($_prog+1))
      echo -ne "\rsplitting collection ${_coll} ($_prog/$_total)"
      # collection depends are added as dependencies
      if [[ $_pkg == collection-* ]]; then
        echo ${_pkg/collection-/texlive-} >> depends-$_coll
      else
        echo $_pkg >> packages-$_coll
        # move files to the corresponding subdir
        _split=`sed -e "0,/^name ${_pkg}$/d;/^$/Q" tlpkg/texlive.tlpdb`
        _files=`echo "$_split" | sed -e "0,/^runfiles/d;/^[a-z]/Q" | grep texmf-dist` || true
        for _file in $_files; do
          # some modules include docs in runfiles
          [[ $_file == texmf-dist/doc/* ]] && continue
          mkdir -p texlive-$_coll/$(dirname $_file)
          mv $_file texlive-$_coll/$(dirname $_file)
        done
        # extract formats
        _fmts=`echo "$_split" | grep "execute AddFormat"` || true
        if [[ ! -z "$_fmts" ]]; then
          echo "$_fmts" | while read -r _fmt; do
            _name=`echo $_fmt | sed 's|.* name=\(\S*\).*|\1|'`
            _engine=`echo $_fmt | sed 's|.* engine=\(\S*\).*|\1|'`
            grep -E "(^| )$_name $_engine" fmtutil.cnf >> $_coll.fmts
          done
        fi
        # extract maps
        _maps=`echo "$_split" | grep -E "execute add(Kanji|Mixed|)Map"` || true
        if [[ ! -z "$_maps" ]]; then
          echo "$_maps" | while read -r _map; do
            grep "${_map/execute add/}" updmap.cfg >> $_coll.maps
          done
        fi
        # extract hyphen rules
        _langs=`echo "$_split" | grep "execute AddHyphen"` || true
        if [[ ! -z "$_langs" ]]; then
          sed -e "0,/from ${_pkg}:/d;/\%/Q" language.dat >> $_coll.dat
          sed -re "0,/from ${_pkg}:/d;/(^--|^})/Q" language.dat.lua >> $_coll.dat.lua
          sed -e "0,/from ${_pkg}:/d;/\%/Q" language.def >> $_coll.def
        fi
        # extract linked scripts
        if [[ ${_pkg} != psutils && "$_split" == *${_pkg}.ARCH* ]]; then
          _links=`sed -e "0,/^name ${_pkg}.x86_64-linux$/d;/^$/Q" tlpkg/texlive.tlpdb | grep "bin/x86_64-linux" | sed -e 's|bin/x86_64-linux||g'`
          for _link in $_links; do
            if [[ $(readlink -m x86_64-linux/$_link) == */scripts/* ]]; then
              mkdir -p ${_coll}-bin
              cp -P x86_64-linux/$_link ${_coll}-bin
              ln -sfn "$(readlink ${_coll}-bin/$_link | sed 's|..\/..|..\/share|')" ${_coll}-bin/$_link
            fi
          done
        fi
      fi
    done
    echo
  done
}

_package() {
  pkgdesc+="`cat pkgdesc-$1`"
  [[ -s depends-$1 ]] && depends+=(`cat depends-$1`)
# jadetex depends on ulem and marvosym
  [[ $1 == formatsextra ]] && depends+=(texlive-plaingeneric texlive-fontsrecommended)
  [[ $1 == latex || $1 == binextra ]] && depends+=(dvisvgm)
  [[ $1 == context ]] && optdepends+=('luametatex: LuaMetaTeX engine')
  [[ $1 == fontutils ]] && optdepends+=('ghostscript: for epstopdf')
  [[ $1 == pictures ]] && optdepends+=('ghostscript: for epspdf')
  [[ $1 == latexextra ]] && optdepends+=('java-runtime: for pdfannotextractor'
                                         'python-pygments: for pygmentex')
  [[ $1 == music ]] && optdepends+=('fontforge: for scripts from the lilyglyphs package')
  if [[ $1 == basic ]]; then
    optdepends+=('perl-tk: for the tlmgr GUI'
                 'biber: for bibliography processing')
    conflicts=(texlive-core)
    provides=(texlive-core)
    replaces=(texlive-core)
  fi
  if [[ $1 == binextra ]]; then
    optdepends+=('ed: for texconfig'
                 'dialog: for texconfig'
                 'java-runtime: for utilities like arara, texplate'
                 'perl-tk: for texdoctk'
                 'perl-yaml-tiny: for latexindent'
                 'perl-file-homedir: for latexindent'
                 'perl-ipc-system-simple: for latex-git-log'
                 'python: for de-macro, dviasm, pythontex'
                 'wdiff: for texdiff')
    conflicts=(git-latexdiff)
    provides=(git-latexdiff)
    replaces=(git-latexdiff)
  fi
  if [[ $1 == mathscience ]]; then
    conflicts=(texlive-science)
    provides=(texlive-science)
    replaces=(texlive-science)
  fi
  if [[ $1 == langother ]]; then
    conflicts=(texlive-langextra)
    provides=(texlive-langextra)
    replaces=(texlive-langextra)
  fi
  if [[ $1 == lang* ]]; then
    groups=(texlive-lang)
  else
    groups=(texlive)
  fi
  for _pkg in `cat packages-$1`; do
    [[ ! ${_collections[@]} =~ (^| )$_pkg($| ) ]] && provides+=(texlive-$_pkg)
  done

  mkdir -p "$pkgdir"/usr/share
  mv texlive-$1/texmf-dist "$pkgdir"/usr/share
  
  [[ -d $1-bin ]] && mv $1-bin "$pkgdir"/usr/bin

  [[ -s $1.fmts ]] && install -Dm644 $1.fmts -t "$pkgdir"/var/lib/texmf/arch/installedpkgs
  [[ -s $1.maps ]] && install -Dm644 $1.maps -t "$pkgdir"/var/lib/texmf/arch/installedpkgs
  [[ -s $1.dat ]] && install -Dm644 $1.dat -t "$pkgdir"/var/lib/texmf/arch/installedpkgs
  [[ -s $1.dat.lua ]] && install -Dm644 $1.dat.lua -t "$pkgdir"/var/lib/texmf/arch/installedpkgs
  [[ -s $1.def ]] && install -Dm644 $1.def -t "$pkgdir"/var/lib/texmf/arch/installedpkgs
  
  if [[ $1 == basic ]]; then
    backup=(etc/texmf/dvipdfmx/dvipdfmx.cfg
            etc/texmf/dvips/config/config.ps
            etc/texmf/tex/generic/tex-ini-files/pdftexconfig.tex
            etc/texmf/web2c/fmtutil.cnf
            etc/texmf/web2c/mktex.cnf
            etc/texmf/web2c/texmf.cnf
            etc/texmf/web2c/updmap-hdr.cfg
            etc/texmf/xdvi/XDvi)
    install -Dm644 09-texlive-fonts.conf -t "$pkgdir"/usr/share/fontconfig/conf.avail/
  # install pacman hooks
    install -Dm644 *.hook -t "$pkgdir"/usr/share/libalpm/hooks/
    install -Dm755 mktexlsr.script "$pkgdir"/usr/share/libalpm/scripts/mktexlsr
    install -Dm755 texlive-fmtutil.script "$pkgdir"/usr/share/libalpm/scripts/texlive-fmtutil
    install -Dm755 texlive-language.script "$pkgdir"/usr/share/libalpm/scripts/texlive-language
    install -Dm755 texlive-updmap.script "$pkgdir"/usr/share/libalpm/scripts/texlive-updmap
  # install tlpkg files needed by tlmgr and texconfig
    install -Dm644 "$srcdir"/tlpkg/TeXLive/* -t "$pkgdir"/usr/share/perl5/vendor_perl/TeXLive/
    install -Dm644 "$srcdir"/tlpkg/texlive.tlpdb -t "$pkgdir"/usr/share/tlpkg
    install -Dm644 "$srcdir"/tlpkg/installer/config.guess -t "$pkgdir"/usr/share/tlpkg/installer
  # remove files that will be autogenerated
    rm "$pkgdir"/usr/share/texmf-dist/web2c/updmap.cfg
    rm "$pkgdir"/usr/share/texmf-dist/web2c/fmtutil.cnf
    rm "$pkgdir"/usr/share/texmf-dist/tex/generic/config/language.{dat,dat.lua,def}
  # copy config files to $TEXMFCONFIG tree
    install -Dm644 "$pkgdir"/usr/share/texmf-dist/dvipdfmx/dvipdfmx.cfg -t "$pkgdir"/etc/texmf/dvipdfmx/
    install -Dm644 "$pkgdir"/usr/share/texmf-dist/dvips/config/config.ps -t "$pkgdir"/etc/texmf/dvips/config/
    install -Dm644 "$pkgdir"/usr/share/texmf-dist/tex/generic/config/language.us "$pkgdir"/etc/texmf/tex/generic/config/language.dat
    install -Dm644 "$pkgdir"/usr/share/texmf-dist/tex/generic/config/language.us.lua "$pkgdir"/etc/texmf/tex/generic/config/language.dat.lua
    echo "}" >> "$pkgdir"/etc/texmf/tex/generic/config/language.dat.lua
    install -Dm644 "$pkgdir"/usr/share/texmf-dist/tex/generic/config/language.us.def "$pkgdir"/etc/texmf/tex/generic/config/language.def
    echo "\uselanguage {USenglish} %%% This MUST be the last line of the file." >> "$pkgdir"/etc/texmf/tex/generic/config/language.def
    install -Dm644 "$pkgdir"/usr/share/texmf-dist/tex/generic/tex-ini-files/pdftexconfig.tex -t "$pkgdir"/etc/texmf/tex/generic/tex-ini-files/
    install -Dm644 "$pkgdir"/usr/share/texmf-dist/web2c/fmtutil-hdr.cnf "$pkgdir"/etc/texmf/web2c/fmtutil.cnf
    install -Dm644 "$pkgdir"/usr/share/texmf-dist/web2c/mktex.cnf -t "$pkgdir"/etc/texmf/web2c/
    install -Dm644 "$pkgdir"/usr/share/texmf-dist/web2c/texmf.cnf -t "$pkgdir"/etc/texmf/web2c/
    install -Dm644 "$pkgdir"/usr/share/texmf-dist/web2c/updmap-hdr.cfg -t "$pkgdir"/etc/texmf/web2c/
    install -Dm644 "$pkgdir"/usr/share/texmf-dist/xdvi/XDvi -t "$pkgdir"/etc/texmf/xdvi/
  fi

  if [[ $1 == binextra ]]; then
    backup=(etc/texmf/chktex/chktexrc)
  # copy config files to $TEXMFCONFIG tree
    install -Dm644 "$pkgdir"/usr/share/texmf-dist/chktex/chktexrc -t "$pkgdir"/etc/texmf/chktex/
  fi

  if [[ $1 == fontutils ]]; then
    backup=(etc/texmf/ttf2pk/ttf2pk.cfg)
  # copy config files to $TEXMFCONFIG tree
    install -Dm644 "$pkgdir"/usr/share/texmf-dist/ttf2pk/ttf2pk.cfg -t "$pkgdir"/etc/texmf/ttf2pk/
  fi
}
  
for _coll in ${_collections[@]}; do
  eval "package_texlive-$_coll() {
    _package $_coll
  }"
done

package_texlive-doc() {
  pkgdesc+='documentation'

  mkdir -p "$pkgdir"/usr/share/doc
  mv texmf-dist/doc/{info,man} "$pkgdir"/usr/share
  mv texmf-dist/doc "$pkgdir"/usr/share/doc/texlive
# Symlink so texdoc can find it
  mkdir -p "$pkgdir"/usr/share/texmf-dist
  ln -s /usr/share/doc/texlive "$pkgdir"/usr/share/texmf-dist/doc
# Fix conflicts with asymptote, psutils, dvisvgm, t1utils
  rm "$pkgdir"/usr/share/info/asy-faq.info
  rm "$pkgdir"/usr/share/man/man1/{asy,dvisvgm,epsffit,extractres,includeres,psbook,psjoin,psnup,psresize,psselect,pstops,psutils,t1ascii,t1asm,t1binary,t1disasm,t1mac,t1unmac}.1
  rm -f "$pkgdir"/usr/share/man{,/man*}/{Makefile,*.pdf}
}
 
package_texlive-meta() {
  pkgdesc='Metapackage to install Tex Live'
  for _coll in ${_collections[@]}; do
    [[ $_coll != lang* ]] && depends+=(texlive-$_coll)
  done
}
